package com.abigdreamer.im.test.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.URL;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;

public class WPDesktop extends JDesktopPane {
	
	private Image wallPaper;
    javax.swing.JLabel lable;
	
	
	public WPDesktop(){
		URL url	=WPDesktop.class.getResource("computer.png");
		javax.swing.ImageIcon icon=new javax.swing.ImageIcon(url);
		lable=new JLabel();
		lable.setIcon(icon);
		lable.setText("我得服务器");
		lable.setToolTipText("我得电脑");
		lable.setBounds(20, 20, 110, 32);
		add(lable, new Integer(10));
		this.wallPaper  = Toolkit.getDefaultToolkit().createImage(
				WPDesktop.class.getResource("Bliss.jpg"));
		MediaTracker tracker = new MediaTracker(this);
		tracker.addImage(wallPaper, 0);
		try {
			tracker.waitForAll();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		lable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            	if(evt.getClickCount()==2){
            		ch();
            	}
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
            	hhh();
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
            	exited();
            }
        });
		
	}
	

	public  void ch(){
		JInternalFrame childFrame = new JInternalFrame("MDI child frame", true);
		childFrame.setClosable(true);
		childFrame.setBounds(100, 100, 200, 200);
		this.add(childFrame, new Integer(10));
		childFrame.show();
	}
	public  void hhh(){
		lable.setBackground(Color.BLUE);
		lable.setBounds(lable.getX()+2, lable.getY()+2, lable.getWidth(), lable.getHeight());
	}
	
	public  void exited(){
		lable.setBounds(lable.getX()-2, lable.getY()-2, lable.getWidth(), lable.getHeight());
	}
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		int width = wallPaper.getWidth(this);
		int height = wallPaper.getHeight(this);
		Dimension size = getSize();
		for (int x = 0; x < size.width; x += width)
			for (int y = 0; y < size.height; y += height)
				g.drawImage(wallPaper, x, y, this);
	}

	public Dimension getPreferredSize() {
		return new Dimension(wallPaper.getWidth(this), wallPaper
				.getHeight(this));
	}
	
	
	public static void main(String[] args) {
		WPDesktop desktop= new WPDesktop();
		JFrame mainFrame = new JFrame("Application main frame");
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.getContentPane().add(desktop);
		mainFrame.pack();
		mainFrame.setVisible(true);
	}

}
